<!DOCTYPE html>
<html>

<head>
    <meta charset="utf-8">
    <title>layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../../lib/layui-v2.6.3/css/layui.css" media="all">
    <link rel="stylesheet" href="../../css/public.css" media="all">
    <style>
        body {
            background-color: #ffffff;
        }
    </style>
</head>

<body>
    <div class="layui-form layuimini-form" lay-filter="addForm">
        <div class="layui-form-item" hidden>
            <label class="layui-form-label required">ID</label>
            <div class="layui-input-block">
                <input type="text" name="positionId" lay-reqtext="职位ID不能为空" placeholder="请输入职位ID" value=""
                    class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label required">职位名称</label>
            <div class="layui-input-inline">
                <input type="text" name="positionName" placeholder="请输入职位名称" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label required">部门</label>
            <div class="layui-input-inline">
                <input type="text" name="department" placeholder="请输入部门" class="layui-input" lay-verify="required">
            </div>
        </div>
        <div class="layui-form-item layui-form-text">
            <label class="layui-form-label">描述</label>
            <div class="layui-input-block">
                <textarea name="description" placeholder="请输入描述" class="layui-textarea"></textarea>
            </div>
        </div>

        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn layui-btn-normal" lay-submit lay-filter="saveBtn">确认保存</button>
            </div>
        </div>
    </div>

    <script src="../../lib/layui-v2.6.3/layui.js" charset="utf-8"></script>
    <script>
        layui.use(['form'], function () {
            var form = layui.form,
                layer = layui.layer,
                $ = layui.$;

            //监听提交
            form.on('submit(saveBtn)', function (data) {
                // console.log(JSON.stringify(data.field))

                $.ajax({
                    url: 'http://localhost:8080/api/job_position?operation=add',
                    type: 'POST',
                    contentType: 'application/json',
                    data: JSON.stringify(data.field),
                    success: function (res) {
                        if (res.code === 0) {
                            layer.alert('保存成功', {
                                title: '操作提示'
                            }, function () {
                                // 刷新父页面
                                parent.location.reload();
                                // 关闭弹出层
                                var index = parent.layer.getFrameIndex(window.name);
                                parent.layer.close(index);
                            });
                        } else {
                            layer.alert('保存失败，请重试', {
                                title: '操作提示',
                                icon: 2
                            });
                        }
                    },
                    error: function () {
                        layer.alert('请求失败，请重试', {
                            title: '操作提示',
                            icon: 2
                        });
                    }
                });
                return false;
            });


            var upload = layui.upload;

            // 监听上传按钮的点击事件
            $('#uploadAvatarBtn').on('click', function () {
                $('#avatarUpload').click(); // 触发文件选择框的点击事件
            });

            // 上传头像
            upload.render({
                elem: '#avatarUpload',
                url: 'http://localhost:8080/file/upload', // 替换成实际的上传接口地址
                accept: 'images', // 只允许上传图片
                exts: 'jpg|png|gif|bmp|jpeg',
                done: function (res) {
                    // 直接使用后端返回的 URL 来更新头像预览
                    if (res.code === 1 && res.data.length > 0) {
                        var imageUrl = res.data[0];
                        $('#avatarPreview').attr('src', imageUrl);
                        // 更新隐藏的输入框的值
                        $('#avatarUpload').val(imageUrl);
                    } else {
                        layer.msg('上传失败', { icon: 2 });
                    }
                }
            });
        });



    </script>
</body>

</html>